package org.swust.monitor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.swust.monitor.config.SecurityConfig;
import org.swust.monitor.entity.R;
import org.swust.monitor.entity.SysUser;
import org.swust.monitor.entity.vo.QSysUser;
import org.swust.monitor.entity.vo.UserStatusVo;
import org.swust.monitor.service.ISysUserService;
import org.swust.monitor.util.SecurityUtils;

import java.time.LocalDateTime;
import java.time.chrono.IsoChronology;
import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
 * 系统用户表 前端控制器
 *
 * @author hanxinxin
 * @since 2024-12-31
 */
@RestController
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private ISysUserService userService;

    /**
     * 通过ID查询用户信息
     *
     * @param id ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    public R<SysUser> getUserById(@PathVariable Integer id) {
        SysUser user = userService.getById(id);
        if (user == null) {
            return R.failed("用户不存在");
        } else {
            return R.ok(user);
        }
    }

    /**
     * 保存用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @PostMapping
    public R<Boolean> saveUser(@RequestBody SysUser user) {
        //  user.setCreateTime(LocalDateTime.now());
        user.setCreatorId(SecurityUtils.getUser().getUserId().toString());
        boolean flag = userService.save(user);
        if (flag) {
            return R.ok(true);
        } else {
            return R.failed("保存失败");
        }
    }


    /**
     * 更新用户信息
     *
     * @param user 用户信息
     * @return R
     */
    @PutMapping
    public R<Boolean> editUser(@Valid @RequestBody SysUser user) {
        boolean result = userService.updateById(user);
        if (result) {
            return R.ok(true);
        } else {
            return R.failed("更新失败");
        }
    }

    /**
     * 通过ID删除用户信息
     *
     * @param id 用户ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delUser(@PathVariable("id") Integer id) {

        boolean flag = userService.removeById(id);
        if (flag) {
            return R.ok(true);
        } else {
            return R.failed("删除失败");
        }
    }

    /**
     * 获取用户列表
     *
     * @return 用户列表
     */
    @GetMapping("/list")
    public R<List<SysUser>> list() {
        return R.ok(userService.list());
    }

    /**
     * 分页查询用户列表
     *
     * @param page 分页对象
     */
    @GetMapping("/page")
    public R<Page<SysUser>> page(Page<SysUser> page) {
        return R.ok(userService.page(page));
    }

    @GetMapping("/page2")
    public R<IPage<SysUser>> page2(Page page, QSysUser qSysUser) {
        return R.ok(userService.pageUser(page, qSysUser));
    }

    /**
     * 根据条件查询用户列表
     *
     * @param qSysUser 查询条件
     * @return
     */
    @GetMapping("/qlist")
    public R<List<SysUser>> list(QSysUser qSysUser) {
        return R.ok(userService.list(qSysUser));
    }


//    public R<List<SysUser>> listByNickname(@RequestParam (required = false)String nickname) {
//
//        return R.ok(userService.listByNickname(nickname));
//    }


    /**
     * 更新用户状态1
     *
     * @param userStatus 用户状态
     * @return R
     */
    @PutMapping("/status")
    public R<Boolean> editStatus(@Valid @RequestBody UserStatusVo userStatus) {
        boolean result = userService.updateStatusById(userStatus.getUserId(), userStatus.getStatus());
        if (result) {
            return R.ok(true);
        } else {
            return R.failed("更新状态失败");
        }
    }


    /**
     * 更新用户状态2
     *
     * @param userStatus 用户状态
     * @return R
     */
    @PutMapping("/Status2")
    public R<Boolean> editStatus2(@Valid @RequestBody UserStatusVo userStatus) {
        SysUser user = new SysUser();
        user.setUserId(userStatus.getUserId());
        user.setStatus(userStatus.getStatus());
        boolean result = userService.updateById(user);
        if (result) {
            return R.ok(true);
        } else {
            return R.failed("更新状态失败");
        }
    }



    /**
     * 重置密码
     * @param userId
     * @return
     */
    @PutMapping("/reset/{userId}")
    public R<Boolean> resetPassword(@PathVariable Integer userId) {
        return userService.resetPassword(userId) > 0 ? R.ok(true) : R.failed("重置密码失败");
    }




}
